<?php

namespace app\models;

use app\logics\Common;

class User extends BaseModel
{

    const STATUS_NORMAL = 1;

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=14, nullable=false)
     */
    public $id;

    /**
     *
     * @var string
     * @Column(type="string", length=32, nullable=false)
     */
    public $mobile;

    /**
     *
     * @var string
     * @Column(type="string", length=512, nullable=false)
     */
    public $password;

    /**
     *
     * @var string
     * @Column(type="string", length=32, nullable=false)
     */
    public $open_id;

    /**
     *
     * @var integer
     * @Column(type="integer", length=10, nullable=false)
     */
    public $created_at;

    /**
     *
     * @var integer
     * @Column(type="integer", length=3, nullable=false)
     */
    public $reg_type;

    /**
     *
     * @var string
     * @Column(type="string", length=32, nullable=false)
     */
    public $nickname;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=false)
     */
    public $icon;

    /**
     *
     * @var integer
     * @Column(type="integer", length=10, nullable=false)
     */
    public $updated_at;

    public $last_login_token;

    public $last_login_ip;

    public $last_login_at;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("user-centor");
        $this->setSource("user");
    }

    public function beforeValidation()
    {
        if (!Common::verifyKenyaMobile($this->mobile)) {
            self::$errorCode = ERROR_MOBILE_NUMBER_INVALID;
            return false;
        }
        //框架自带的功能，导致这些数据为空的时候不能 执行
        if(!$this->id) {
            $this->updated_at = $this->created_at = time();
            $this->open_id = 'aa';
        }
        return true;
    }

    public function beforeCreate()
    {
        $passwordLength = strlen($this->password);
        if ($passwordLength < 6 || $passwordLength > 32) {
            self::$errorCode = ERROR_PASSWORD_LENGTH_INVALID;
            return false;
        }
        $exists = $this->select(['mobile' => $this->mobile], 'mobile')->numRows();
        if ($exists) {
            self::$errorCode = ERROR_MOBILE_NUMBER_EXISTS;
            return false;
        }
        if (empty($this->nickname)) {
            $this->nickname = 'player_' . bin2hex($this->getRandom()->bytes(4));
        }
        $this->password = $this->getSecurity()->hash($this->password,2);
        //为用户生成16位长度的open_id
        $this->open_id = $this->getRandom()->uuid();
        $this->open_id = str_replace('-', '', $this->open_id);
        $this->open_id = substr($this->open_id, 0, 15);
        return true;
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'user';
    }

    public function getIcon()
    {
        return $this->icon;
    }

}
